<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class page2_css_process extends Admin_Controller
{
    var $access_supperadmin = 1;
	function sortColumn()
	{
		global $CI;
		
		// model
		$CI->load->module_model('page2_css.page2_css_list_model');
		$col = $CI->uri->segment($CI->page2_css_list_model->item_segment);
		
		$order = array(''=>'desc', 'desc'=>'asc', 'asc'=>'desc');
		foreach ($CI->page2_css_list_model->columns as $k=>$v)
		{
			if (strcmp($k, $col) == 0)
			{
				$CI->page2_css_list_model->cache->add($k, $order[$CI->page2_css_list_model->cache->load($k)]);
			} else
				$CI->page2_css_list_model->cache->add($k, '');
		}
		
		// go to original page
		redirect($CI->page2_css_list_model->url_prefix.'/'.$CI->page2_css_list_model->start);
	}
	
	function deleteAll()
	{
		global $CI;
		
		// model
		$CI->load->module_model('page2_css.page2_css_list_model');
		$flag = 0;
		if (isset($_POST['uid']) && is_array($_POST['uid']))
		{
			foreach ($_POST['uid'] as $id)
			{
				if ($CI->page2_css_list_model->check_delete($id))
				{
					$flag = 1;
					$CI->db->delete(PAGE_CSS, array(PAGE_CSS_PAGE_CSS_ID=>$id));			
				}
			}
		}
		if ($flag)
			// message
			$CI->page2_css_list_model->cache->add($CI->page2_css_list_model->msg_cache_prefix, $CI->lang->line('list_delete_all'));
		else
			// message
			$CI->page2_css_list_model->cache->add($CI->page2_css_list_model->msg_cache_prefix, $CI->lang->line('list_cannot_delete')." : ".$CI->page2_css_list_model->error_string);
		
		// go to original page
		redirect($CI->page2_css_list_model->url_prefix.'/'.$CI->page2_css_list_model->start);
	}
	
	function deleteOne()
	{
		global $CI;
		
		// model
		$CI->load->module_model('page2_css.page2_css_list_model');
		
		$id = $CI->uri->segment($CI->page2_css_list_model->item_segment);
		if ($CI->page2_css_list_model->check_delete($id))
		{
			$CI->db->delete(PAGE_CSS, array(PAGE_CSS_PAGE_CSS_ID=>$id));
		
			// message
			$CI->page2_css_list_model->cache->add($CI->page2_css_list_model->msg_cache_prefix, $CI->lang->line('list_delete'));
		} else
			// message
			$CI->page2_css_list_model->cache->add($CI->page2_css_list_model->msg_cache_prefix, $CI->lang->line('list_cannot_delete')." : ".$CI->page2_css_list_model->error_string);
		
		// go to original page
		redirect($CI->page2_css_list_model->url_prefix.'/'.$CI->page2_css_list_model->start);
	}	
	
	function saveItem()
	{
		global $CI;
		
		// model
		$CI->load->module_model('page2_css.page2_css_detail_model');
		
		// validate
		$CI->load->library('form_validation');
        $CI->form_validation->set_rules('css_file', 'Name', 'trim|required');
        if ($CI->form_validation->run() == FALSE)
		{
			// message
			$CI->page2_css_detail_model->cache->add($CI->page2_css_detail_model->msg_cache_prefix, $CI->lang->line('detail_cannot_save')." : ".$CI->form_validation->error_string());
			$CI->page2_css_detail_model->cache->add('post', $_POST);

			// go to original page
			redirect($CI->page2_css_detail_model->url_prefix.'/'.$CI->page2_css_detail_model->start.'/'.$CI->page2_css_detail_model->item);
		}
		
		// save?
		if (!$CI->page2_css_detail_model->check())
		{
			// message
			$CI->page2_css_detail_model->cache->add($CI->page2_css_detail_model->msg_cache_prefix, $CI->lang->line('detail_cannot_save')." : ".$CI->lang->line('_name_duplicate'));
			$CI->page2_css_detail_model->cache->add('post', $_POST);

			// go to original page
			redirect($CI->page2_css_detail_model->url_prefix.'/'.$CI->page2_css_detail_model->start.'/'.$CI->page2_css_detail_model->item);
		}
		
		if ($CI->page2_css_detail_model->hasPage2_css())
		{
			// update
			$sql = "UPDATE ".PAGE_CSS." SET";
			$sql .= " ".PAGE_CSS_CSS_FILE."=".$CI->db->escape($CI->input->post('css_file'));
			$sql .= ", ".PAGE_CSS_NOTE."=".$CI->db->escape($CI->input->post('note'));
			//$sql .= " WHERE ".PAGE_CSS_FRANCHISE_ID."=".$CI->page2_css_detail_model->admin_id();
			$sql .= " WHERE ".PAGE_CSS_PAGE_CSS_ID."=".$CI->page2_css_detail_model->item;
			$CI->db->query($sql);
		} else {
			// insert
			$data = array(
				PAGE_CSS_CSS_FILE=>$CI->input->post('css_file'),
				PAGE_CSS_NOTE=>$CI->input->post('note'),
				PAGE_CSS_FRANCHISE_ID=>$CI->page2_css_detail_model->admin_id(),
				//PAGE_CSS_PAGE_CSS_ID=>$CI->page2_css_detail_model->item,
			);
			$CI->db->insert(PAGE_CSS, $data);
		}

		// message
		$CI->page2_css_detail_model->cache->add($CI->page2_css_detail_model->msg_cache_prefix, $CI->lang->line('detail_save'));
		
		// go to original page
		redirect($CI->page2_css_detail_model->url_prefix2.'/'.$CI->page2_css_detail_model->start);
	}		
}

?>